home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / comm / misc / avmnfaxsrc1_33.lha / config.c < prev    next >
C/C++ Source or Header  |  1994-05-25  |  1KB  |  82 lines

  1. /* $Header$ */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #include "config_proto.h"
  7.  
  8. /* EXPORTS */
  9.  
  10. /* IMPORTS */
  11.  
  12. /* LOCALS */
  13.  
  14. int getInt(FILE* fp) {
  15.   char temp[32];
  16.   int result;
  17.   fgets(temp, sizeof(temp), fp);
  18.   sscanf(temp, "%d", &result);
  19.   return result;
  20. }
  21.  
  22. void mystrncpy(char* dest, char* src, int len) {
  23.   strncpy(dest, src, len - 1);
  24.   dest[len - 1] = '\0';
  25. }
  26.  
  27. void killNewLine(char* line) {
  28.   int i;
  29.   int len = strlen(line);
  30.  
  31.   for (i = 0; i < len; i++)
  32.     if (line[i] == '\n' || line[i] == '\r') line[i] = '\0';
  33. }
  34.  
  35. void getString(FILE* fp, char* buffer, int size) {
  36.   static char temp[2000];
  37.   fgets(temp, sizeof(temp), fp);
  38.   mystrncpy(buffer, temp, size);
  39.   
  40.   killNewLine(buffer);
  41. }
  42.  
  43. void extractVariableValue(char* s, char* var, char* val) {
  44.   char* equals;
  45.  
  46.   equals = strchr(s, '=');
  47.   if (equals) {
  48.     // found
  49.     mystrncpy(var, s, (equals - s) + 1);
  50.     strcpy(val, equals + 1);
  51.   } else {
  52.     strcpy(var, "");
  53.     strcpy(val, "");
  54.   }
  55. }
  56.  
  57. void loadConfig(char* fileName, loaderFuncPtr processConfig) {
  58.   static char buffer[1024];
  59.   static char variable[32];
  60.   static char value[512];
  61.   FILE* fp;
  62.  
  63.   fp = fopen(fileName, "r");
  64.  
  65.   if (!fp) {
  66. //    printf("Unable to open config file\n");
  67.     return;
  68.   }
  69.  
  70.   while (!feof(fp)) {
  71.     getString(fp, buffer, sizeof(buffer));
  72.     if (feof(fp)) break;
  73.     extractVariableValue(buffer, variable, value);
  74.     processConfig(variable, value);
  75.   }
  76.  
  77.   fclose(fp);
  78.  
  79.   return;
  80. }
  81.  
  82.